// ==========================================================================
// GFast自动生成logic操作代码。
// 生成日期：2024-04-24 10:28:09
// 生成路径: internal/app/business/logic/agents.go
// 生成人：周志雄
// desc:代理商
// company:云南奇讯科技有限公司
// ==========================================================================

package logic

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/tiger1103/gfast/v3/api/v1/business"
	"github.com/tiger1103/gfast/v3/internal/app/business/dao"
	"github.com/tiger1103/gfast/v3/internal/app/business/model"
	"github.com/tiger1103/gfast/v3/internal/app/business/model/do"
	"github.com/tiger1103/gfast/v3/internal/app/business/service"
	"github.com/tiger1103/gfast/v3/internal/app/system/consts"
	"github.com/tiger1103/gfast/v3/library/liberr"
)

func init() {
	service.RegisterAgents(New())
}

func New() *sAgents {
	return &sAgents{}
}

type sAgents struct{}

func (s *sAgents) List(ctx context.Context, req *business.AgentsSearchReq) (listRes *business.AgentsSearchRes, err error) {
	listRes = new(business.AgentsSearchRes)
	err = g.Try(ctx, func(ctx context.Context) {
		m := dao.Agents.Ctx(ctx).WithAll()

		if req.Name != "" {
			m = m.Where(dao.Agents.Columns().Name+" like ?", "%"+req.Name+"%")
		}
		if req.State != "" {
			m = m.Where(dao.Agents.Columns().State+" = ?", req.State)
		}
		listRes.Total, err = m.Count()
		liberr.ErrIsNil(ctx, err, "获取总行数失败")
		if req.PageNum == 0 {
			req.PageNum = 1
		}
		listRes.CurrentPage = req.PageNum
		if req.PageSize == 0 {
			req.PageSize = consts.PageSize
		}
		order := "id asc"
		var res []*model.AgentsInfoRes
		err = m.Fields(business.AgentsSearchRes{}).Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取数据失败")
		listRes.List = make([]*model.AgentsListRes, len(res))
		for k, v := range res {
			listRes.List[k] = &model.AgentsListRes{
				Id:        v.Id,
				Name:      v.Name,
				State:     v.State,
				CreatedAt: v.CreatedAt,
			}
		}
	})
	return
}

func (s *sAgents) GetById(ctx context.Context, id int) (res *model.AgentsInfoRes, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		err = dao.Agents.Ctx(ctx).WithAll().Where(dao.Agents.Columns().Id, id).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取信息失败")
	})
	return
}

func (s *sAgents) Add(ctx context.Context, req *business.AgentsAddReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.Agents.Ctx(ctx).Insert(do.Agents{
			Name:  req.Name,
			State: 1,
		})
		liberr.ErrIsNil(ctx, err, "添加失败")
	})
	return
}

func (s *sAgents) Edit(ctx context.Context, req *business.AgentsEditReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.Agents.Ctx(ctx).WherePri(req.Id).Update(do.Agents{
			Name:  req.Name,
			State: req.State,
		})
		liberr.ErrIsNil(ctx, err, "修改失败")
	})
	return
}

func (s *sAgents) Delete(ctx context.Context, ids []int) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.Agents.Ctx(ctx).Delete(dao.Agents.Columns().Id+" in (?)", ids)
		liberr.ErrIsNil(ctx, err, "删除失败")
	})
	return
}
